package com.sbio.udiapp.ui;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.LogUtils;
import com.sbio.udiapp.BuildConfig;
import com.sbio.udiapp.R;
import com.sbio.udiapp.common.bean.RequestUniParam;
import com.sbio.udiapp.databinding.ActivityLoginBinding;
import com.sbio.udiapp.net.api.ApiService;
import com.sbio.udiapp.net.bean.AjaxResult;
import com.sbio.udiapp.util.DefaultSharedPreferencesUtil;
import com.sbio.udiapp.util.LogUtil;
import com.sbio.udiapp.util.RsaUtils;
import com.sbio.udiapp.util.SharedPreferencesUtil;
import com.sbio.udiapp.util.ToastUtil;

import java.util.HashMap;

import javax.inject.Inject;

import butterknife.OnClick;
import dagger.hilt.android.AndroidEntryPoint;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@AndroidEntryPoint
public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding binding;

    @Inject
    ApiService apiService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.submit.setOnClickListener(v -> {
            try {
                submit();
            } catch (Exception e) {
                LogUtils.d(e.getMessage());
            }
        });
        if (BuildConfig.DEBUG) {
            //开发环境自动登录
            new Handler().postDelayed(() -> {
                binding.username.setText("s01041");
                binding.password.setText("123456");
//                binding.username.setText("admin");
//                binding.password.setText("admin123");
                try {
//                    submit();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }, 1000);
        }
    }

    public void submit() throws Exception {
        String username = binding.username.getText().toString();
        String password = binding.password.getText().toString();

        /*String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArt8/y1+hCBwYlG171W6v" +
                "5trwXnzqfECzVgYkKMC02Ya025vAQMIDbmMLY+azUmjYTOnmBdOA7KNhlMOVeRKD" +
                "0Z8sqTl/5Qty3/BaSo+X3wTGUtXdTCnjYnATRxZSRUiUWRXnQklT4SfCDrpEMDdL" +
                "TWAbUwrz/VXRnVgMjLFqIGsYGh7991U1t3pyTZvLndH22sOkzcwF6amxQN9MAjKR" +
                "/s1gi0g/bAr3qSaf7sQ3TKGeBYLU4OfYY+5Kf4dpX+s+TzjwlVt9kmuresZHwr8Q" +
                "8DLFmcrjdglKjlUZY+0Gv9f1NIdc3pIEev8OvhlVcKHMkRJmxgn87yubQo44xYNu" +
                "OQIDAQAB";
        password = RsaUtils.encryptByPublicKey(publicKey, password);*/
        String requestUniParam = RequestUniParam.create("点击了登录按钮").toJson();
//        Call<AjaxResult> login = apiService.login(username, password, requestUniParam);
        HashMap<Object, Object> body = new HashMap<>();
        body.put("username", username);
        body.put("password", password);
        body.put("tenantId", "000000");
        body.put("clientId", "e5cd7e4891bf95d1d19206ce24a7b32e");
        body.put("grantType", "password");
        Call<AjaxResult> login = apiService.login2(body, requestUniParam);
        login.enqueue(new Callback<AjaxResult>() {
            @Override
            public void onResponse(Call<AjaxResult> call, Response<AjaxResult> response) {
                LogUtil.file("login2", response);
                if (!response.isSuccessful()) {
                    ToastUtil.show("登录失败,服务器异常，请稍后重试");
                    return;
                }
                AjaxResult ajaxResult = response.body();
                boolean success = ajaxResult.isSuccess();
                if (success) {
                    ToastUtil.show("登录成功");
                    SharedPreferencesUtil.saveBoolean("login", true);
                    SharedPreferencesUtil.saveString("username", username);
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    // 销毁当前活动
                    finish();
                } else {
                    ToastUtil.show(ajaxResult.getMsg());
                }
            }

            @Override
            public void onFailure(Call<AjaxResult> call, Throwable t) {

                ToastUtil.show(t.getMessage());
            }
        });
    }
}